package com.tubeonfire.entity;

import java.util.Date;
import java.util.List;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.google.appengine.api.datastore.Text;

@PersistenceCapable
public class Playlist {

	@PrimaryKey
	@Persistent
	private String id;
	@Persistent
	private Text alias;
	@Persistent
	private String userFederatedId;
	@Persistent
	private Text title;
	@Persistent
	private Text description;
	@Persistent
	private Date doc;
	@Persistent
	private Date update;
	@Persistent
	private Text pictureBlobKey;
	@Persistent
	private List<Text> listTubes;
	@Persistent
	private int status;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Text getAlias() {
		return alias;
	}

	public void setAlias(Text alias) {
		this.alias = alias;
	}

	public String getUserFederatedId() {
		return userFederatedId;
	}

	public void setUserFederatedId(String userFederatedId) {
		this.userFederatedId = userFederatedId;
	}

	public Text getTitle() {
		return title;
	}

	public void setTitle(Text title) {
		this.title = title;
	}

	public Text getDescription() {
		return description;
	}

	public void setDescription(Text description) {
		this.description = description;
	}

	public Date getDoc() {
		return doc;
	}

	public void setDoc(Date doc) {
		this.doc = doc;
	}

	public Date getUpdate() {
		return update;
	}

	public void setUpdate(Date update) {
		this.update = update;
	}

	public Text getPictureBlobKey() {
		return pictureBlobKey;
	}

	public void setPictureBlobKey(Text pictureBlobKey) {
		this.pictureBlobKey = pictureBlobKey;
	}

	public List<Text> getListTubes() {
		return listTubes;
	}

	public void setListTubes(List<Text> listTubes) {
		this.listTubes = listTubes;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public boolean isOk() {
		if (this.title == null || this.title.getValue().equalsIgnoreCase("")) {
			return false;
		}
		if (this.description == null
				|| this.description.getValue().equalsIgnoreCase("")) {
			return false;
		}
		if (this.id == null) {
			return false;
		}
		if (this.alias == null || this.alias.getValue().equalsIgnoreCase("")) {
			return false;
		}
		if (this.userFederatedId == null
				|| this.userFederatedId.equalsIgnoreCase("")) {
			return false;
		}
		return true;
	}

	public String toString() {
		StringBuilder content = new StringBuilder();
		if (this.id != null) {
			content.append("<id>" + this.id + "</id>\n");
		}
		if (this.alias != null) {
			content.append("<alias>" + this.alias.getValue() + "</alias>\n");
		}
		if (this.title != null) {
			content.append("<title>" + this.title.getValue() + "</title>\n");
		}
		if (this.pictureBlobKey != null) {
			content.append("<pictureBlobKey>" + this.pictureBlobKey.getValue()
					+ "</pictureBlobKey>\n");
		}
		if (this.description != null) {
			content.append("<description>" + this.description.getValue()
					+ "</description>\n");
		}
		return content.toString();
	}

	public void transformString(String plString) {
		Document doc = Jsoup.parse(plString);
		Elements tmp;
		tmp = doc.select("id");
		if (tmp != null) {
			this.id = tmp.text();
		}
		tmp = doc.select("alias");
		if (tmp != null) {
			this.alias = new Text(tmp.text());
		}
		tmp = doc.select("pictureBlobKey");
		if (tmp != null) {
			this.pictureBlobKey = (new Text(tmp.text()));
		}
		tmp = doc.select("title");
		if (tmp != null) {
			this.title = (new Text(tmp.text()));
		}
		tmp = doc.select("description");
		if (tmp != null) {
			this.description = (new Text(tmp.text()));
		}
	}

}
